###############################################################
# Values, Taboos, and Votes: 
# How Basic Human Values Affect Populist Electoral Support
#
# Ugur Ozdemir, Marc S. Jacob
# 2021
# SUPPLEMENTAL MATERIAL II
###############################################################

# loading packages and data  #

# if package installation required remove "#"
# install.packages(c("gridExtra","car","tidyverse","data.table","foreign","lavaan","memisc","stargazer"),dependencies = TRUE)

pckgs <-
  c(
    "gridExtra",
    "dplyr",
    "car",
    "tidyverse",
    "data.table",
    "memisc",
    "lavaan",
    "stargazer"
  )
lapply(pckgs, require, character.only = TRUE)

# loading data
gr17 <- read.spss("ZA6804_v6-0-0.sav",to.data.frame = TRUE,use.value.labels = FALSE) # loading data
gr17 <- filter(gr17, w1b == "1") # select observations from wave 1

sem.data <-
  dplyr::select(
    gr17,
    kp1_170,
    kp1_2100,
    kp1_2090a,
    kp1_190ba,
    kp1_1090,
    kp1_1130,
    kp1_1210,
    kp1_1250,
    kp1_1290,
    kp1_1411,
    kp1_2591,
    kp1_3910,
    kp1_2320,
    kpx_2290,
    kpx_2280,
    kp1_1500,
    kp1_3320e,
    kp1_3320m,
    kp1_3320c,
    kp1_3320h,
    kp1_3320u,
    kp1_3320n,
    kp1_3320q,
    kp1_3680,
    kp1_2580,
    kp9_200ba,
    ostwest
  ) # select variables

#### VARIABLE PREPARATION ####

# rename variables #

oldnames <-
  c(
    "kp1_1090",
    "kp1_3910",
    "kp1_2320",
    "kp1_2591",
    "kpx_2280",
    "kp1_2580",
    "kp1_1411",
    "kp1_1130",
    "kp1_1290",
    "kp1_1250",
    "kp1_1210",
    "kp1_1500"
  ) # rename variables
newnames <-
  c(
    "SocEcon1",
    "Relig",
    "Educ",
    "Income",
    "Gender",
    "Class",
    "SecPriv1",
    "Immigrat1",
    "Climate1",
    "EuInteg1",
    "MigInteg1",
    "LeftRight"
  )
setnames(sem.data, oldnames, newnames)

# data manipulation #

sem.data$Age <- 2016 - sem.data$kpx_2290 # compute age variable

sem.data$Relig <-
  recode(sem.data$Relig, "1=5;2=4;3=3;4=2;5=1") #recoding religion (5 (new) = very religious)
sem.data$Gender <-
  recode(sem.data$Gender, "2=1;1=0") #recoding gender (1 (new) = female)

# set party preference #

sem.data$AfD <- ordered(ifelse(sem.data$kp1_190ba == 322, 1, 0))
sem.data$Left <- ordered(ifelse(sem.data$kp1_190ba == 7, 1, 0))
sem.data$SPD <- ordered(ifelse(sem.data$kp1_190ba == 4, 1, 0))
sem.data$Greens <- ordered(ifelse(sem.data$kp1_190ba == 6, 1, 0))
sem.data$CDU <- ordered(ifelse(sem.data$kp1_190ba == 1, 1, 0))
sem.data$FDP <- ordered(ifelse(sem.data$kp1_190ba == 5, 1, 0))

sem.data$populist <- memisc::recode(sem.data$kp1_190ba,
                                    1 <- c(322, 7),
                                    NA <- NA,
                                    otherwise = 0)

# set issue opinions ordered (for ordinal regression of values on issue opinions) #

sem.data$Immigrat1 <- as.numeric(sem.data$Immigrat1)
sem.data$SecPriv1 <- as.numeric(sem.data$SecPriv1)
sem.data$EuInteg1 <- as.numeric(sem.data$EuInteg1)
sem.data$SocEcon1 <- as.numeric(sem.data$SocEcon1)
sem.data$MigInteg1 <- as.numeric(sem.data$MigInteg1)
sem.data$Climate1 <- as.numeric(sem.data$Climate1)
sem.data$Climate1 <- as.numeric(sem.data$Climate1)
sem.data$LeftRight <- as.numeric(sem.data$LeftRight)

#### FACTOR ANALYSIS ####

model <- '
# 1. measurement model
sec =~ kp1_3320e + kp1_3320m
univ =~ kp1_3320c + kp1_3320h + kp1_3320u
conf =~ kp1_3320n + kp1_3320q
'

model <- sem(model, data = sem.data)
scores <- lavPredict(model, type = "lv")

sem.data %>% drop_na(kp1_3320e,
                     kp1_3320m,
                     kp1_3320c,
                     kp1_3320h,
                     kp1_3320u,
                     kp1_3320n,
                     kp1_3320q) -> sem.data # drop missing NAs
sem.data <- cbind(sem.data, scores)


#### PROBIT REGRESSIONS #####

## AfD

AfD1 <-
  glm(
    AfD ~ Age + Income + Gender + Educ + Relig + ostwest,
    data = sem.data,
    family = binomial(link = "probit")
  )

AfD2 <-
  glm(
    AfD ~ Immigrat1 + SecPriv1 + EuInteg1 + SocEcon1 + MigInteg1 + Climate1 + LeftRight + Age + Income + Gender + Educ + Relig + ostwest,
    data = sem.data,
    family = binomial(link = "probit")
  )

AfD3 <-
  glm(
    AfD ~ Immigrat1 + SecPriv1 + EuInteg1 + SocEcon1 + MigInteg1 + Climate1 + LeftRight + Age + Income + Gender + Educ + Relig + ostwest + sec + univ + conf,
    data = sem.data,
    family = binomial(link = "probit")
  )

stargazer(
  AfD1,
  AfD2,
  AfD3,
  type = "latex",
  covariate.labels = c(
    "Immigration",
    "Privacy",
    "European integration",
    "Socio-economy",
    "Integration",
    "Climate",
    "Left-Right",
    "Age",
    "Income",
    "Gender",
    "Educational level",
    "Religiousity",
    "East/west-born",
    "Security",
    "Universalism",
    "Conformity"
  )
)

## Left

Left1 <-
  glm(
    Left ~ Age + Income + Gender + Educ + Relig + ostwest,
    data = sem.data,
    family = binomial(link = "probit")
  )

Left2 <-
  glm(
    Left ~ Immigrat1 + SecPriv1 + EuInteg1 + SocEcon1 + MigInteg1 + Climate1 + LeftRight + Age + Income + Gender + Educ + Relig + ostwest,
    data = sem.data,
    family = binomial(link = "probit")
  )

Left3 <-
  glm(
    Left ~ Immigrat1 + SecPriv1 + EuInteg1 + SocEcon1 + MigInteg1 + Climate1 + LeftRight + Age + Income + Gender + Educ + Relig + ostwest + sec + univ + conf,
    data = sem.data,
    family = binomial(link = "probit")
  )

stargazer(
  Left1,
  Left2,
  Left3,
  type = "latex",
  covariate.labels = c(
    "Immigration",
    "Privacy",
    "European integration",
    "Socio-economy",
    "Integration",
    "Climate",
    "LeftRight",
    "Age",
    "Income",
    "Gender",
    "Educational level",
    "Religiousity",
    "East/west-born",
    "Security",
    "Universalism",
    "Conformity"
  )
)

## CDU

CDU1 <-
  glm(
    CDU ~ Age + Income + Gender + Educ + Relig + ostwest,
    data = sem.data,
    family = binomial(link = "probit")
  )

CDU2 <-
  glm(
    CDU ~ Immigrat1 + SecPriv1 + EuInteg1 + SocEcon1 + MigInteg1 + Climate1 + LeftRight + Age + Income + Gender + Educ + Relig + ostwest,
    data = sem.data,
    family = binomial(link = "probit")
  )

CDU3 <-
  glm(
    CDU ~ Immigrat1 + SecPriv1 + EuInteg1 + SocEcon1 + MigInteg1 + Climate1 + LeftRight + Age + Income + Gender + Educ + Relig + ostwest + sec + univ + conf,
    data = sem.data,
    family = binomial(link = "probit")
  )

stargazer(
  CDU1,
  CDU2,
  CDU3,
  type = "latex",
  covariate.labels = c(
    "Immigration",
    "Privacy",
    "European integration",
    "Socio-economy",
    "Integration",
    "Climate",
    "Left-Right",
    "Age",
    "Income",
    "Gender",
    "Educational level",
    "Religiousity",
    "East/west-born",
    "Security",
    "Universalism",
    "Conformity"
  )
)


## SPD

SPD1 <-
  glm(
    SPD ~ Age + Income + Gender + Educ + Relig + ostwest,
    data = sem.data,
    family = binomial(link = "probit")
  )

SPD2 <-
  glm(
    SPD ~ Immigrat1 + SecPriv1 + EuInteg1 + SocEcon1 + MigInteg1 + Climate1 + LeftRight + Age + Income + Gender + Educ + Relig + ostwest,
    data = sem.data,
    family = binomial(link = "probit")
  )

SPD3 <-
  glm(
    SPD ~ Immigrat1 + SecPriv1 + EuInteg1 + SocEcon1 + MigInteg1 + Climate1 + LeftRight + Age + Income + Gender + Educ + Relig + ostwest + sec + univ + conf,
    data = sem.data,
    family = binomial(link = "probit")
  )

stargazer(
  SPD1,
  SPD2,
  SPD3,
  type = "latex",
  covariate.labels = c(
    "Immigration",
    "Privacy",
    "European integration",
    "Socio-economy",
    "Integration",
    "Climate",
    "Left-Right",
    "Age",
    "Income",
    "Gender",
    "Educational level",
    "Religiousity",
    "East/west-born",
    "Security",
    "Universalism",
    "Conformity"
  )
)


## Greens

Greens1 <-
  glm(
    Greens ~ Age + Income + Gender + Educ + Relig + ostwest,
    data = sem.data,
    family = binomial(link = "probit")
  )

Greens2 <-
  glm(
    Greens ~ Immigrat1 + SecPriv1 + EuInteg1 + SocEcon1 + MigInteg1 + Climate1 + LeftRight + Age + Income + Gender + Educ + Relig + ostwest,
    data = sem.data,
    family = binomial(link = "probit")
  )

Greens3 <-
  glm(
    Greens ~ Immigrat1 + SecPriv1 + EuInteg1 + SocEcon1 + MigInteg1 + Climate1 + LeftRight + Age + Income + Gender + Educ + Relig + ostwest + sec + univ + conf,
    data = sem.data,
    family = binomial(link = "probit")
  )

stargazer(
  Greens1,
  Greens2,
  Greens3,
  type = "latex",
  covariate.labels = c(
    "Immigration",
    "Privacy",
    "European integration",
    "Socio-economy",
    "Integration",
    "Climate",
    "Left-Right",
    "Age",
    "Income",
    "Gender",
    "Educational level",
    "Religiousity",
    "East/west-born",
    "Security",
    "Universalism",
    "Conformity"
  )
)

## FDP

FDP1 <-
  glm(
    FDP ~ Age + Income + Gender + Educ + Relig + ostwest,
    data = sem.data,
    family = binomial(link = "probit")
  )

FDP2 <-
  glm(
    FDP ~ Immigrat1 + SecPriv1 + EuInteg1 + SocEcon1 + MigInteg1 + Climate1 + LeftRight + Age + Income + Gender + Educ + Relig + ostwest,
    data = sem.data,
    family = binomial(link = "probit")
  )

FDP3 <-
  glm(
    FDP ~ Immigrat1 + SecPriv1 + EuInteg1 + SocEcon1 + MigInteg1 + Climate1 + LeftRight + Age + Income + Gender + Educ + Relig + ostwest + sec + univ + conf,
    data = sem.data,
    family = binomial(link = "probit")
  )

stargazer(
  FDP1,
  FDP2,
  FDP3,
  type = "latex",
  covariate.labels = c(
    "Immigration",
    "Privacy",
    "European integration",
    "Socio-economy",
    "Integration",
    "Climate",
    "Left-Right",
    "Age",
    "Income",
    "Gender",
    "Educational level",
    "Religiousity",
    "East/west-born",
    "Security",
    "Universalism",
    "Conformity"
  )
)
